﻿@using System.Data.Entity.Infrastructure.Pluralization
@using ARchGL.Declaration.Platform.Web.Models
@using TDF.Core.Configuration
@model Faurecia.WM.Service.Dtos.MachineDto
<div class="page-bar">
    <ul class="page-breadcrumb">
        <li>
            <span id="module">@Configs.Instance.GetValue("SystemName")</span>
            <i class="fa fa-angle-right"></i>
        </li>
        <li>
            <span id="action"></span>
        </li>
    </ul>
</div>
<div class="portlet light portlet-fit portlet-datatable bordered">
    <div class="portlet-title">
        <div class="caption">
            <i class="icon-settings"></i>
            <span class="caption-subject sbold uppercase">生产列表-机位(@Model.Number)</span>
        </div>
        <div class="actions">
            <a href="javascript:;" id="btnMouldAdd" class="btn btn-default btn-sm">
                <i class="fa fa-plus"></i> 按模具新增
            </a>
            @if ((bool)ViewBag.IsOneToOne)
            {
                <a href="javascript:;" class="btn btn-default btn-sm btnAdd">
                    <i class="fa fa-plus"></i> 新增
                </a>
            }
            <a href="javascript:;" id="btnClearAll" class="btn btn-default btn-sm" style="color: red">
                <i class="fa fa-remove"></i> 删除当前机位的所有生产计划
            </a>
        </div>
    </div>
    <div class="portlet-body">
        <table id="table-pagination-plan" data-url="@Url.Action("GetProductionRulePagedList", new {MachineId = Model.Id})"
               data-height="430"
               data-form-id="defaultForm"
               data-show-refresh="true"
               data-page-size="5"
               data-page-list="[5, 10, 20, 50, 100, 200]">
            <thead>
            <tr>
                <th data-align="center" data-formatter="opIndexColumns">序号</th>
                @{
                    var columns = (List<ColumnModel>) ViewBag.Columns;
                }
                @if (columns.Count > 0)
                {
                    foreach (var column in columns)
                    {
                        <th data-align="center" data-field="@column.Field">@column.FieldDisplay</th>
                    }
                }
                <th data-align="center" data-field="CreatedTime">加入时间</th>
                <th data-align="center" data-formatter="opColumns">操作</th>
            </tr>
            </thead>
        </table>

    </div>
</div>
<div class="portlet light portlet-fit portlet-datatable bordered">
    <div class="portlet-title">
        <div class="caption">
            <i class="icon-settings"></i>
            <span class="caption-subject sbold uppercase">计划堆叠列表-机位(@Model.Number)</span>
        </div>
    </div>
    <div class="portlet-body">
        <table id="table-pagination-bbb" data-url="@Url.Action("GetProductionBBBPagedList", new {MachineId = Model.Id })"
               data-height="430"
               data-form-id="defaultForm"
               data-show-refresh="true"
               data-page-size="5"
               data-page-list="[5, 10, 20, 50, 100, 200]">
            <thead>
                <tr>
                    <th data-align="center" data-formatter="opMachineColumns">机位</th>
                    @{
                        var bbbcolumns = (List<ColumnModel>)ViewBag.BBBColumns;
                    }
                    @if (columns.Count > 0)
                    {
                        foreach (var column in bbbcolumns)
                        {
                            <th data-align="center" data-field="@column.Field">@column.FieldDisplay</th>
                        }
                    }
                    <th data-align="center" data-field="GroupCount">堆叠数</th>
                    <th data-align="center" data-field="BBBStackCount">产品总数</th>
                    <th data-formatter="opBBBColumns" data-align="center">操作</th>
                </tr>
            </thead>
        </table>

    </div>
</div>
<!-- END PAGE BAR -->
<!-- 参数设置 弹出框模板 -->
<div id="dialogTemplet" class="hide">
    <form class="form-horizontal" id="product-form" method="post">
        <div class="form-body">
            <div class="row">
                <div class="col-md-10">
                    <div class="form-group">
                        <label class="control-label col-md-2">产品</label>
                        <div class="col-md-10">
                            <input type="hidden" name="MachineId" id="MachineId" class="form-control" value="@Model.Id"/>
                            <select id="ProductId" class="form-control" name="ProductId"></select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<div id="dialogMouldTemplet" class="hide">
    <form class="form-horizontal" id="mould-form" method="post">
        <div class="form-body">
            <div class="row">
                <div class="col-md-10">
                    <div class="form-group">
                        <label class="control-label col-md-2">模具</label>
                        <div class="col-md-10">
                            <select id="ProductIds" class="form-control" name="ProductIds"></select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<!-- End 弹出框模板 -->

@section scripts{
    <style>
        .fa-arrow-down, .fa-arrow-up {
            cursor: pointer;
            margin: 5px;
        }

            .fa-arrow-down.disabled, .fa-arrow-up.disabled {
                color: gray;
                cursor: auto;
            }
    </style>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                url: '@Url.Action("GetCanProductionProducts")',
                //data: $.param({ machineId: $("#MachineId").val() }),
                data: { machineId: $("#MachineId").val() },
                success: function (result) {
                    if (result.Success) {
                        for (var i = 0; i < result.Value.length; i++) {
                            var product = result.Value[i];
                            $("#ProductId").append('<option value="' + product.Id + '">' + product.Value + '</option>');
                        }
                    } else {
                        showToast(3, '系统消息', result.Message);
                    }
                }
            });
            $.ajax({
                url: '@Url.Action("GetCanProductionMoulds")',
                //data: $.param({ machineId: $("#MachineId").val() }),
                data: { machineId: $("#MachineId").val() },
                success: function (result) {
                    if (result.Success) {
                        for (var mouldName in result.Value) {
                            var mouid = mouldName;
                            var ids = result.Value[mouid].join(',');
                            $("#ProductIds").append('<option value="' + ids + '">' + mouid + '</option>');
                        }
                    } else {
                        showToast(3, '系统消息', result.Message);
                    }
                }
            });
        });
        $("#table-pagination-plan").bootstrapTable({
            onLoadSuccess: function (data) {
                $("#table-pagination-plan .fa-arrow-up:first").parent().addClass("disabled");
                $("#table-pagination-plan .fa-arrow-down:last").parent().addClass("disabled");
                $("#table-pagination-plan .fa-arrow-down").parent().click(function () {
                    var $item = $(this);
                    if ($item.hasClass("disabled")) {
                        return;
                    }
                    var $nextItem = $($item.parents("tr").next());
                    var sourceId = $item.attr("data-id");
                    var targetId = $nextItem.find(".fa-arrow-up").parent().attr("data-id");
                    SwapAdvert(sourceId, targetId);
                });
                $("#table-pagination-plan .fa-arrow-up").parent().click(function () {
                    var $item = $(this);
                    if ($item.hasClass("disabled")) {
                        return;
                    }
                    var $preItem = $($item.parents("tr").prev());
                    var sourceId = $item.attr("data-id");
                    var targetId = $preItem.find(".fa-arrow-up").parent().attr("data-id");
                    SwapAdvert(sourceId, targetId);
                });
            }
        });
        var opIndexColumns = function (value, row, index) {
            return index + 1;
        };
        var opColumns = function (value, row, index) {
            var btn1 = '<a href="javascript:;" data-id="' + row.ProductionPlanId + '" class="btn default"> 升级<i class="fa fa-arrow-up"></i></a>';
            var btn2 = '<a href="javascript:;" data-id="' + row.ProductionPlanId + '" class="btn default"> 降级<i class="fa fa-arrow-down"></i></a>';
            var btn3 = '<a href="javascript:remove(\'' + row.ProductionPlanId + '\');" class="btn default"> 删除<i class="fa fa-remove"></i></a>';
            return btn1 + btn2 + btn3;
        };
        var remove = function (id) {
            bootbox.confirm("是否确认删除？", function (result) {
                if (!result) {
                    return;
                }
                $.ajax({
                    type: "POST",
                    url: "@Url.Action("DeleteProductionPlan")",
                    data: { planId: id },
                    async: false,
                    success: function (result) {
                        if (result.Success) {
                            $("#table-pagination-plan").bootstrapTable('refresh');
                        } else {
                            showToast(3, '系统消息', result.Message);
                        }
                    }
                });
            });
        };

        function SwapAdvert(sourceId, targetId) {
            $.ajax({
                type: "POST",
                url: "@Url.Action("SwitchProductionPlanSort")",
                data: { planIdSource: sourceId, planIdTargetId: targetId },
                async: false,
                success: function (result) {
                    if (result.Success) {
                        $("#table-pagination-plan").bootstrapTable('refresh');
                    } else {
                        showToast(3, '系统消息', result.Message);
                    }
                }
            });
        }

        var showMouldDialog = function (parameter) {

            var settingParamDialog = bootbox.dialog({
                title: '新增计划',
                message: $('#dialogMouldTemplet').html(),
                className: 'my-modal',
                buttons: {
                    success: {
                        label: '关 闭',
                    },
                    main: {
                        label: '保 存',
                        callback: function () {
                            var formDoc = $(".my-modal .modal-dialog form");
                            formDoc.submit();
                            return false;
                        }
                    }
                }
            });

            //初始化弹出框
            settingParamDialog.init(function () {
                var formDoc = $(".my-modal .modal-dialog form");
                customFormValidate(formDoc, {}, function (form) {

                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("AddProductionPlans")',
                        data: { productIds: formDoc.find("select[name=ProductIds]").val().split(','), machineId: $("#MachineId").val() },
                        async: false,
                        success: function (result) {
                            if (result.Success) {
                                bootbox.hideAll(); //关闭弹出框
                                showToast(0, '系统提示', '保存成功');
                                $('#table-pagination-plan').bootstrapTable('refresh');
                            } else {
                                showToast(3, '系统提示', result.Message);
                            }
                        },
                        error: function (request) {
                            showToast(3, '系统提示', ' 操作失败!');
                        }
                    });

                });
            });
        };


        var showDialog = function (parameter) {

            var settingParamDialog = bootbox.dialog({
                title: '新增计划',
                message: $('#dialogTemplet').html(),
                className: 'my-modal',
                buttons: {
                    success: {
                        label: '关 闭',
                    },
                    main: {
                        label: '保 存',
                        callback: function () {
                            var formDoc = $(".my-modal .modal-dialog form");
                            formDoc.submit();
                            return false;
                        }
                    }
                }
            });

            //初始化弹出框
            settingParamDialog.init(function () {
                var formDoc = $(".my-modal .modal-dialog form");
                customFormValidate(formDoc, {}, function (form) {

                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("AddProductionPlan")',
                        data: { productId: formDoc.find("select[name=ProductId]").val(), machineId: $("#MachineId").val() },
                        async: false,
                        success: function (result) {
                            if (result.Success) {
                                bootbox.hideAll(); //关闭弹出框
                                showToast(0, '系统提示', '保存成功');
                                $('#table-pagination-plan').bootstrapTable('refresh');
                            } else {
                                showToast(3, '系统提示', result.Message);
                            }
                        },
                        error: function (request) {
                            showToast(3, '系统提示', ' 操作失败!');
                        }
                    });

                });
            });
        };

        var showClearConfirmDialog = function() {
            bootbox.confirm("您确定要清空该机位上的所有生产计划吗？", function (result) {
                if (result) {
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("ClearAllProduction")',
                        data: { machineId: $("#MachineId").val() },
                        async: false,
                        success: function (result) {
                            if (result.Success) {
                                showToast(0, '系统提示', '清空成功');
                                $('#table-pagination-plan').bootstrapTable('refresh');
                            } else {
                                showToast(3, '系统提示', result.Message);
                            }
                        },
                        error: function (request) {
                            showToast(3, '系统提示', ' 操作失败!');
                        }
                    });
                }
            });
        }

        $('.btnAdd').click(showDialog);
        $('#btnMouldAdd').click(showMouldDialog);
        $('#btnClearAll').click(showClearConfirmDialog);
        $("#table-pagination-bbb").bootstrapTable();
        var opBBBColumns = function (value, row, index) {
            return  '<a href="javascript:ReleaseBBB(\'' + row.StackIds.join(',') + '\');" class="btn default"> 释放<i class="fa fa-remove"></i></a>';
        };

        var opMachineColumns = function (value, row, index) {
            return "@Model.Number";
        }

        var ReleaseBBB = function(stackIds) {
            var ids = stackIds.split(',');
            $.ajax({
                url: '@Url.Action("ReleaseBBB")',
                type: 'post',
                async: false,
                data: { StackIds: ids ,MachineId:$("#MachineId").val()},
                success:function(result) {
                    if (result.Success) {
                        bootbox.hideAll(); //关闭弹出框
                        showToast(0, '系统提示', '释放成功');
                        $('#table-pagination-bbb').bootstrapTable('refresh');
                        $('#table-pagination-plan').bootstrapTable('refresh');
                    } else {
                        showToast(3, '系统提示', result.Message);
                    }
                }

            });
        }
    </script>
}
